package com.ruoyi.hr.controller.hr;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.Companys;
import com.ruoyi.hr.domain.Tree;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.CompanysService;
import com.ruoyi.hr.service.DepartmentsService;
import com.ruoyi.hr.service.OrgsService;
import com.ruoyi.system.domain.SysUser;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/orgs")
public class OrgsController extends BaseController {


    @Autowired
    private DepartmentsService departmentsService;

    @Autowired
    AuthorityService authorityService;

    @Autowired
    private CompanysService companysService;

    @Autowired
    private JinPengController jinPengController;

    @Autowired
    private OrgsService orgsService;

    private String prefix = "hr/orgs";

    /**
     * 跳转分页页面
     */
    @GetMapping()
    public String user() {
        return prefix + "/orgs";
    }


    /**
     * 获取分页数据以及数据总量
     */
    @PostMapping("/list")
    @ResponseBody
    public void list() {
    }

    @GetMapping("findAllTree")
    @ResponseBody
    public Tree findAllTree(Companys companys, String type) {
        List<String> AllID = new ArrayList<String>();
        String CompID = null;
        if (companys.getCompID() != null) {
            CompID = companys.getCompID().intValue() - 10000 + "";
            AllID.add(CompID);
        } else {
            SysUser user = ShiroUtils.getSysUser();
            List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
            for (Authority authority : authorityList) {
                AllID.add(authority.getCompID().toString());
            }
        }

        Tree t = departmentsService.findAllTree(AllID, CompID, type);
        return t;
    }


    /**
     * 根据公司ID取得包括子公司下所有的ID,根据部门ID取得包括子部门下的所有ID
     *
     * @param CompID 公司祖ID,部门祖ID
     * @param type   ID类型 Comp=公司  depart=部门
     * @return 所有公司ID的List, 所有部门ID的List
     */
    public List<String> GetCompanyandSubsidiaryID(String CompID, String type) {
        List<String> SubsidiaryList = new ArrayList<String>();//用于取得所有子公司ID,子部门ID
        List<String> AllId = new ArrayList<String>();//用于记录所有ID
        AllId.add(CompID);
        SubsidiaryList.add(CompID);
        do {
            if ("Comp".equals(type)) {
                SubsidiaryList = companysService.findSubsidiaryID(SubsidiaryList);//获取所有子公司ID
            } else {
                SubsidiaryList = departmentsService.findSubdepartmentID(SubsidiaryList);//获取所有子部门ID
            }
            for (String id : SubsidiaryList) {
                AllId.add(id);
            }
        } while (SubsidiaryList.size() != 0);
        return AllId;
    }


    /**
     * 跳转分页页面
     */
    @GetMapping("/toOrgs")
    public String toOrgs(ModelMap mmap) {
        //公司
        mmap.put("companys", companysService.findAllCompanys(null));
        return prefix + "/toOrgs";
    }

    @RequestMapping("/findOrgs")
    @ResponseBody
    public Map<String, Object> findOrgs(@RequestParam Map map) {
        Map<String, Object> OrgsMap = orgsService.findOrgs(map);
        return OrgsMap;
    }

    @RequestMapping("/open")
    public String open(@RequestParam Map map, ModelMap mmap){
        mmap.put("id",map.get("id"));
        mmap.put("term",map.get("term"));
        mmap.put("type",map.get("type"));
        return prefix + "/open";
    }
}
